﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ShowFile : System.Web.UI.Page
{
	private UploadFileComponent clsUploadFileComp = new UploadFileComponent();
	private PX.UploadFile.UploadFileComponent clsComp = new PX.UploadFile.UploadFileComponent();
	private PX.File.FileComponent clsFileComp = new PX.File.FileComponent();
	private PX.File.File clsFile = new PX.File.File();
	PX.UploadFile.UploadFile clsUploadFile = new PX.UploadFile.UploadFile();

	private int nFileID;		// 要显示的文件ID

	protected void Page_Load(object sender, EventArgs e)
	{
				
		byte[] bFileContent;
		string[] strArrShow;
		Boolean bShow;			// 上传的文件是否直接显示，true直接显示

		strArrShow = new string[2];
		bShow = false;

		strArrShow[0] = "text/html";
		strArrShow[1] = "message/rfc822";

		nFileID = Convert.ToInt32(Request.QueryString["FileID"].ToString());

		clsFile =(PX.File.File) clsFileComp.GetOneByID(nFileID);

		clsUploadFile =(PX.UploadFile.UploadFile) clsComp.GetOneByID(clsFile.FileID);

		if (clsUploadFile != null)
		{

			bFileContent = clsUploadFile.Content;
			Response.Clear();
			Response.AddHeader("Content-Type", clsUploadFile.Type);


			for (int i = 0; i < strArrShow.Length; i++)
			{
				if (clsUploadFile.Type == strArrShow[i])
				{
					bShow = true;
					break;
				}
			}
			if (bShow)
			{
				Response.AddHeader("Content-Disposition", "inline;");
			}
			else
			{
				Response.AddHeader("Content-Disposition", "inline;");
				HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + CommonFunction.UTF_FileName(clsUploadFile.FileName) + "\";");
			}
			//Response.AddHeader("Content-Disposition", "inline;");

			Response.BinaryWrite(bFileContent);
			Response.Flush();
			Response.End();
		}
		else
		{
			//Response.Redirect(("/Files/" + clsFile.Path));
			Response.Redirect(clsFile.Path);
		}
	}
}
